import { getItemForPk, getConfigSupportShareFromPK } from '@/config/product';

interface ISelDevice extends IDevice {
  isSelected: boolean;
  count?: number; // 已分享人数
}


/**
 * 分享类型
 */
enum ShareTypeEnum {
  MyShare = 'MyShare', // 我分享的
  Shared = 'Shared', // 接受别人分享的
}

function isSameObject(a: any, b: any) {
  return a === b;
}

// 不重复加入同个元素到数组
function noRepeatAddItemToArray({ list, item, isSame = isSameObject }:
  { list: Array<any>, item: any, isSame?: (item1: any, item2: any) => boolean }) {
  if (list) {
    const index = list.findIndex((i) => isSame(i, item));
    if (index === -1) {
      list.push(item);
    }
  }
}

// 从数组中删除元素
function deleteItemFromArray({ list, item, isSame = isSameObject }:
  { list: Array<any>, item: any, isSame?: (item1: any, item2: any) => boolean }) {
  if (list) {
    const index = list.findIndex((i) => isSame(i, item));
    if (index !== -1) {
      list.splice(index, 1);
    }
  }
}

// 判断数组（arr1）元素是不是全部在另一个数组（arr2）
function isAllInArray({ arr1, arr2, isSame = isSameObject }:
  { arr1: Array<any>, arr2: Array<any>, isSame?: (item1: any, item2: any) => boolean }) {
  for (let index = 0; index < arr1.length; index++) {
    const element = arr1[index];
    if (element) {
      const targetIndex = arr2.findIndex((i) => isSame(element, i));
      if (targetIndex === -1) {
        return false;
      }
    }
  }
  return true;
}

function filterDeviceCanShare(device: IDevice) {
  // 1才代表是设备主人，切这款产品支持分享的，才返回true
  // if (device.deviceSourceType === 1 || device.deviceSourceType === 3) {
  if (device.deviceSourceType === 1) {
    // return true;
    return getConfigSupportShareFromPK(device.productKey);
  }
  return false;
}

export { ShareTypeEnum, noRepeatAddItemToArray, deleteItemFromArray, isAllInArray, filterDeviceCanShare, ISelDevice };
